package com.astn.bks;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.astn.adapter.booking.MessageAdapter;
import com.astn.adapter.booking.POfferAdapter;
import com.astn.bks.util.BksConstants;
import com.astn.bks.util.DateUtils;
import com.astn.bks.util.JsonUtils;
import com.astn.bks.util.WebserviceUtils;
import com.astn.components.ActionItem;
import com.astn.components.QuickAction;
import com.astn.entity.booking.QuoteMessage;
import com.astn.entity.booking.QuoteOffer;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class Message extends Activity {

	private ListView lsvMessage;
	private Button btnNext;
	private Button btnBack;
	private TextView txvPage;
	private ArrayList<QuoteMessage> lstMessage = new ArrayList<QuoteMessage>();
	private QuickAction newRequestMsgQuick;
	private QuickAction newOfferMsgQuick;
	private QuickAction newMessageMsgQuick;
	private int currentPage;
	private int totalPage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.messagelist);
		lsvMessage = (ListView) findViewById(R.id.lsvTop);
		btnBack = (Button) findViewById(R.id.btnPreviousPage);
		btnNext = (Button) findViewById(R.id.btnNextPage);
		txvPage = (TextView) findViewById(R.id.txvPage);
		currentPage=0;
		getNumberPage();
		txvPage.setText(String.valueOf(currentPage + 1) + "/"
				+ String.valueOf(totalPage));
		reloadList();
		ActionItem aitemViewMessage = new ActionItem(BksConstants.VIEW_MESSAGE,
				"View message", getResources().getDrawable(R.drawable.ic_view));
		ActionItem aitemViewRequest = new ActionItem(BksConstants.VIEW_REQUEST,
				"View request", getResources().getDrawable(R.drawable.ic_view));
		ActionItem aitemViewOffer = new ActionItem(BksConstants.VIEW_OFFER,
				"View offer", getResources().getDrawable(R.drawable.ic_view));
		ActionItem aitemPostOffer = new ActionItem(BksConstants.POST_OFFER,
				"Post offer", getResources().getDrawable(R.drawable.ic_offer));
		ActionItem aitemBookOffer = new ActionItem(BksConstants.BOOK_OFFER,
				"Book", getResources().getDrawable(R.drawable.ic_book));
		ActionItem aitemReply = new ActionItem(BksConstants.REPLY, "Reply",
				getResources().getDrawable(R.drawable.ic_reply));
		MessageAdapter adap = new MessageAdapter(getApplicationContext(),
				lstMessage);
		lsvMessage.setAdapter(adap);
		// New request message quickaction
		newRequestMsgQuick = new QuickAction(this);
		newRequestMsgQuick.addActionItem(aitemViewRequest);
		newRequestMsgQuick.addActionItem(aitemPostOffer);
		newRequestMsgQuick.addActionItem(aitemReply);

		// New offer quickaction
		newOfferMsgQuick = new QuickAction(this);
		newOfferMsgQuick.addActionItem(aitemViewOffer);
		newOfferMsgQuick.addActionItem(aitemBookOffer);
		newOfferMsgQuick.addActionItem(aitemReply);

		// New normal msg quick action
		newMessageMsgQuick = new QuickAction(this);
		newMessageMsgQuick.addActionItem(aitemViewMessage);
		newMessageMsgQuick.addActionItem(aitemReply);

		lsvMessage.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (lstMessage.get(position).getMessagetype()) {
				case QuoteMessage.NEW_OFFER:
					newOfferMsgQuick.show(view);
					break;
				case QuoteMessage.NEW_REQUEST:
					newRequestMsgQuick.show(view);
					break;
				case QuoteMessage.NORMAL_MESSAGE:
				case QuoteMessage.OFFER_FAIL:
				case QuoteMessage.OFFER_BOOKED:
					newMessageMsgQuick.show(view);
					break;

				default:
					break;
				}
				// newMessageMsgQuick.show(view);
			}

		});

	}

	private void getNumberPage() {
		Object obj = null;
		Map<String, Object> mapPara = new HashMap<String, Object>();
		mapPara.put("PAGESIZE", BksConstants.PAGESIZE);
		try {
			obj = WebserviceUtils.call("com_xmlrpc", "getNumberPageMessage",
					mapPara);
			String str = (String) obj;
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			Float f = Float.parseFloat(obj.toString());
			totalPage = f.intValue();
		} catch (Exception e) {
			totalPage = 0;
		}
	}

	private void reloadList() {
		lstMessage = new ArrayList<QuoteMessage>();
		Object resultObj = null;
		Map<String, Object> mapPara = new HashMap<String, Object>();
		mapPara.put("PAGE", currentPage);
		mapPara.put("PAGESIZE", BksConstants.PAGESIZE);
		try {
			resultObj = WebserviceUtils.call("com_xmlrpc", "getMessage",
					mapPara);
			String str = (String) resultObj;
		} catch (Exception e) {

		}
		if (resultObj != null) {
			QuoteMessage[] gArr = null;
			try {
				gArr = JsonUtils.gson.fromJson((String) resultObj,
						QuoteMessage[].class);
			} catch (Exception e) {
				e.printStackTrace();
			}
			lstMessage = new ArrayList<QuoteMessage>();
			if (gArr != null) {
				lstMessage.addAll(Arrays.asList(gArr));
			}
			// if (arr != null) {
			// for (int i = 0; i < arr.size(); i++) {
			// JSONObject obj = (JSONObject) arr.get(i);
			// try {
			// QuoteTown tmpTown = new QuoteTown();
			// tmpTown.setCitycode((String) obj.get("citycode"));
			// tmpTown.setCityname((String) obj.get("cityname"));
			// tmpTown.setCitynumber((String) obj.get("citynumber"));
			// tmpTown.setCountryid(Integer.parseInt((String) obj
			// .get("countryid")));
			// tmpTown.setId(Integer.parseInt((String) obj.get("id")));
			// tmpTown.setRegionid(Integer.parseInt((String) obj
			// .get("regionid")));
			// tmpTown.setStateid(Integer.parseInt((String) obj
			// .get("stateid")));
			// lstTown.add(tmpTown);
			// } catch (Exception e) {
			// // TODO: handle exception
			// }
			// }
			// }
		}

		MessageAdapter adap = new MessageAdapter(getApplicationContext(),
				lstMessage);
		lsvMessage.setAdapter(adap);

	}

}
